package com.lanswon.qzsmk.controller.inventoryManageController;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.annotation.SystemControllerLog;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.InventoryException;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.service.systemManageService.InventoryManagementService;
import com.lanswon.qzsmk.vo.StorageRegRecVo;

import ch.qos.logback.classic.Logger;

@Controller
@RequestMapping(value = "/inventoryIncomingMagOfBranch")
public class InventoryIncomingMagOfBranchController {
	private final static Logger logger = (Logger) LoggerFactory.getLogger(InventoryIncomingMagOfBranchController.class);
	
	@Autowired
	InventoryManagementService ims;
	
	/**
	 * 子模块：网点接收配送并入库 
	 * 跳转网点接收页面
	 * @return
	 */
	@RequiresPermissions("inventoryIncomingMagOfBranch:index")
	@RequestMapping(name = "网点接收页面,inventoryIncomingMagOfBranch:index", value = "/queryCardReceiving", method = { RequestMethod.POST, RequestMethod.GET })
	@SystemControllerLog(description = "跳转网点接收页面")
	public String queryCardReceiving(HttpServletRequest request) {
		return "CardReceiving";
	}

	/**
	 * 子模块：网点接收配送并入库 
	 * 网点接收页面加载
	 * @return
	 */
	@RequiresPermissions("inventoryIncomingMagOfBranch:getCardReceiving")
	@RequestMapping(name = "网点接收得卡片信息,inventoryIncomingMagOfBranch:getCardReceiving",value = "/getCardReceiving", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "网点接收页面加载")
	public Map<String, Object> getCardReceiving(HttpServletRequest request, Integer pageNum, Integer numPerPage) {
		logger.debug("页面currentPage==>" + pageNum);
		logger.debug("页面numPerPage==>" + numPerPage);
		if (pageNum == null && numPerPage == null) {
			pageNum = 1;
			numPerPage = 10;
		}
		// 接收前画面穿过来数据
		String distributeTaskNo = request.getParameter("distributeTaskNo");
		//String applyBranchNo = request.getParameter("netpointnumber");
		// 装有画面信息的map
		Map<String, String> map = new HashMap<String, String>();
		map.put("distributeTaskNo", distributeTaskNo);
		//map.put("applyBranchNo", applyBranchNo);
		DwzPage<StorageRegRecVo> srrv = ims.getCardReceiving(map, pageNum, numPerPage);
		Map<String, Object> rMap = new HashMap<>();
		rMap.put("srrv", srrv);
		return rMap;
	}

	/**
	 * 子模块：网点接收配送并入库 
	 * 获取网点接收配送信息
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequiresPermissions("inventoryIncomingMagOfBranch:cardReceiving")
	@RequestMapping(name = "接收登记页面,inventoryIncomingMagOfBranch:cardReceiving",value = "/cardReceiving", method = RequestMethod.GET)
	@SystemControllerLog(description = "获取网点接收配送信息")
	public String cardReceiving(HttpServletRequest request, String parm) throws UnsupportedEncodingException {
		logger.debug("param==>" + parm);
		List<StorageRegRecVo> srrv = ims.querycardReceiving(parm);
		request.getSession().setAttribute("srrv", srrv);
		return "CardReceivingInfo";
	}

	/**
	 * 子模块：网点接收配送并入库 
	 * 返回接收信息
	 * @param request
	 * @return
	 */
	@RequiresPermissions("inventoryIncomingMagOfBranch:cardReceivingInfo")
	@RequestMapping(name = "卡片信息,inventoryIncomingMagOfBranch:cardReceivingInfo",value = "/cardReceivingInfo", method = RequestMethod.GET)
	@ResponseBody
	@SystemControllerLog(description = "返回接收信息")
	public Map<String, Object> cardReceivingInfo(HttpServletRequest request) {
		Map<String, Object> rmap = new HashMap<>();
		rmap.put("srrv", request.getSession().getAttribute("srrv"));
		request.getSession().removeAttribute("");
		logger.debug("cardReceivingInfo==>" + rmap);
		return rmap;
	}

	/**
	 * 子模块：网点接收配送并入库 
	 * 提交信息
	 * @param request
	 * @return
	 * @throws InventoryException 
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("inventoryIncomingMagOfBranch:cardReceivingRegister")
	@RequestMapping(name = "网点入库登记,inventoryIncomingMagOfBranch:cardReceivingRegister",value = "/cardReceivingRegister", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "提交信息")
	public Map<String, Object> cardReceivingRegister(String[] cardReceivingtasked) throws InventoryException {
		Map<String, Object> map = new HashMap<>();
		// 前画面组装成的参数
		String[] paramString = cardReceivingtasked;
		logger.debug("前画面传递的参数==>" + paramString);
		String checkedeParam = null;
		for (int i = 0; i < paramString.length; i++) {
			if (i == 0) {
				checkedeParam = paramString[0];
			} else {
				checkedeParam = checkedeParam + "$" + paramString[i];
			}
		}
		logger.debug("选中的参数有===>" + checkedeParam);
		// 打印参数的组装map
		Map<String, Object> rMap = null;

		rMap = ims.CardReceivingRegist(checkedeParam);
		Map<String,Object> pramMap = new HashMap<String,Object>();
		pramMap.put("applyType", rMap.get("credential_type"));
		map.put("ipAddress", rMap.get("ipAddress"));
		map.put("storageNo", rMap.get("storageNo"));
		map.put("max", "true");
		map.put("title", rMap.get("title"));
		map.put("rel", "receiveTask");
		map.put("statusCode", "200");
		map.put("message", "入库成功");
		map.put("param", pramMap);
		
		
		map.put("jumpToTabid", "queryCardReceiving");
		map.put("jumpToUrl", "inventoryIncomingMagOfBranch/queryCardReceiving");
		map.put("jumpToTile", "网点接收配送入库");
		map.put("url", "printCredentials/toStoragePdfView");
		return map;
	}
}
